package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.RecordAdapter;
import flc.ast.bean.RecordBean;
import flc.ast.databinding.ActivityRecordBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.bean.StkResBean;

public class RecordActivity extends BaseAc<ActivityRecordBinding> {

    private List<RecordBean> listShow = new ArrayList<>();
    private List<RecordBean> listSel = new ArrayList<>();
    private RecordAdapter recordAdapter;
    private boolean isEdit;

    @Override
    protected int onCreate() {
        return R.layout.activity_record;
    }

    @Override
    public void onBackPressed() {
        if (isEdit) {
            cancelEdit();
        } else {
            finish();
        }
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivRecordBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivRecordEdit.setOnClickListener(this);
        mDataBinding.tvDownload.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);

        mDataBinding.rvRecordList.setLayoutManager(new GridLayoutManager(mContext, 2));
        recordAdapter = new RecordAdapter();
        recordAdapter.setEdit(isEdit);
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivRecordEdit://编辑
                isEdit = true;
                recordAdapter.setEdit(isEdit);
                recordAdapter.notifyDataSetChanged();
                mDataBinding.llEdit.setVisibility(View.VISIBLE);
                break;
            case R.id.tvDownload://下载
                if (listSel.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_download));
                    return;
                }
                download();
                break;
            case R.id.tvDelete://删除
                if (listSel.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_record));
                    return;
                }
                delete();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (recordAdapter.getItem(position).isSelected()) {
                recordAdapter.getItem(position).setSelected(false);
                listSel.remove(recordAdapter.getItem(position));
            } else {
                recordAdapter.getItem(position).setSelected(true);
                listSel.add(recordAdapter.getItem(position));
            }
            recordAdapter.notifyItemChanged(position);
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecord;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new RecordBean(file.getPath()));
            }
        }
        if (listShow.size() > 0) {
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.tvRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.tvRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    private void download() {
        showDialog(getString(R.string.download_ing));
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                //从此文件夹删除
                for (RecordBean recordBean : listSel) {
                    FileP2pUtil.copyPrivateImgToPublic(mContext, recordBean.getPath());
                }
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                cancelEdit();
                dismissDialog();
                ToastUtils.showShort(getString(R.string.download_suc));
            }
        });
    }

    private void delete() {
        showDialog(getString(R.string.delete_ing));
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                //从此文件夹删除
                for (RecordBean recordBean : listSel) {
                    FileUtils.delete(recordBean.getPath());
                }
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                cancelEdit();
                ToastUtils.showShort(getString(R.string.delete_suc));
            }
        });
    }

    private void cancelEdit() {
        isEdit = false;
        listSel.clear();
        recordAdapter.setEdit(isEdit);
        getData();
        mDataBinding.llEdit.setVisibility(View.GONE);
    }

}